package com.easyselection.interceptor;


import com.easyselection.utils.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 方法在请求处理之前被调用
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(UserHolder.getUser() == null){
            //进行拦截
            //401状态码:由于服务器无法验证用户身份或者用户没有权限访问请求的资源
            response.setStatus(401);
            return false;
        }
        //说明有用户, 放行即可
        return true;
    }
}
